from django.contrib import admin

# Register your models here.

from . import models

class SKUAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.save()
        from celery_tasks.html.task import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.id)

class SKUSpecificationAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.save()
        from celery_tasks.html.task import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.sku_id)
        
    def delete_model(self, request, obj):
        sku_id = obj.sku.id
        obj.delete()
        from celery_tasks.html.task import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(sku_id)
        
        
class SKUImageAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.save()
        from celery_tasks.html.task import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(obj.sku.id)
        
        sku = obj.sku
        if not sku.default_image_url:
            sku.default_image_url = obj.default_image_url
            sku.save()
            
    def delete_model(self, request, obj):
        sku_id = obj.sku.id
        obj.delete()
        from celery_tasks.html.task import generate_static_sku_detail_html
        generate_static_sku_detail_html.delay(sku_id)
        
        

admin.site.register(models.GoodsCategory)
admin.site.register(models.GoodsChannel)
admin.site.register(models.Goods)
admin.site.register(models.Brand)
admin.site.register(models.GoodsSpecification)
admin.site.register(models.SpecificationOption)
admin.site.register(models.SKU)
admin.site.register(models.SKUSpecification)
admin.site.register(models.SKUImage)